package com.example.login.Controller;

import com.example.login.Mapper.GoodsMap;
import com.example.login.Pojo.*;
import com.example.login.Service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class MerchantCenterController //中转控制器
{
    //上架商品
    @RequestMapping("/merchantOrder")
    public String order()
    {
        return"商家中心/我的订单";
    }
    @RequestMapping("/merchantPC")
    public String pc(Model model)
    {
        model.addAttribute("pc",new PC());
        model.addAttribute("good", new Goods());
        System.out.println("x");
        return"商家中心/上架电脑商品";
    }
    @RequestMapping("/merchantMobile")
    public String mobile(Model model)
    {
        model.addAttribute("mobile",new MobilePhone());
        model.addAttribute("good", new Goods());
        return"商家中心/上架手机商品";
    }
    @RequestMapping("/merchantMP3")
    public String mp3(Model model)
    {
        model.addAttribute("mp3",new MP3());
        model.addAttribute("good",new Goods());
        return"商家中心/上架MP3商品";
    }

    //查看商品
    @Autowired
    GoodsMap goodsMap;
    @RequestMapping("/merchantLook")
    public String Look(Model model)
    {
        List<mp3Goods> mp3= goodsMap.getMp3Goods();
        List<PCGoods> pc=goodsMap.getPCGoods();
        List<mobileGoods> mobile=goodsMap.getMobileGoods();
        System.out.println(mp3);
        System.out.println(pc);
        System.out.println(mobile);
        model.addAttribute("mp3Goods",mp3);
        model.addAttribute("pcGoods",pc);
        model.addAttribute("mobileGoods",mobile);
        System.out.println("查看商品");
        return"商家中心/查看商品";
    }
    @Autowired
    ItemService us;
    @RequestMapping("/merchant/status/{id}/{sku_code}")
    public String update(Model model, @PathVariable("id")int id,
                           @PathVariable("sku_code")String sku_code)
    {
        System.out.println(id+" "+sku_code);
        us.update(id,sku_code);
        //添加数据进入model层
        return "redirect:/merchantLook";
    }

    //删除商品
    @Autowired
    ItemService itemService;
    @RequestMapping("/merchant/delete/{id}/{sku_code}")
    public String delete(Model model, @PathVariable("id")int id,
                           @PathVariable("sku_code")String sku_code)
    {
        itemService.delete(id,sku_code);
        List<mp3Goods> mp3= goodsMap.getMp3Goods();
        List<PCGoods> pc=goodsMap.getPCGoods();
        List<mobileGoods> mobile=goodsMap.getMobileGoods();
        model.addAttribute("mp3Goods",mp3);
        model.addAttribute("pcGoods",pc);
        model.addAttribute("mobileGoods",mobile);
        return "redirect:/merchantLook";
    }
    @RequestMapping("/merchantAna")
    public String ana(HttpSession session,Model model)
    {
        Seller seller= (Seller) session.getAttribute("seller");
        int seller_id =seller.getId();
        ArrayList<Goods> goods =goodsMap.getGoodsBySellerId(seller_id);
        model.addAttribute("goods",goods);
        System.out.println(goods);
        return "商家中心/数据分析";
    }

}
